package com.example.demo.controller;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;

import com.example.demo.entity.*;
import com.example.demo.mapper.StudentPaperMapper;
import com.example.demo.service.IStudentPaperService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * @descreption:阅卷管理
 * @author: xhr
 * @date: 2022/9/03
 */
@RestController
@RequestMapping("/studentPaper")
public class StudentPaperController {
    @Resource
    StudentPaperMapper studentPaperMapper;
    @Resource
    private IStudentPaperService studentPaperService;
//    //增
//    @PostMapping
//    public Result<?> save(@RequestBody StudentPaper studentPaper) {
//
//        StudentPaper res=studentPaperMapper.selectOne(new QueryWrapper<StudentPaper>().eq("user_id", studentPaper.getUserId()).eq("exam_id",studentPaper.getExamId()));
//        if (res == null) {
//            studentPaper.setTime(DateUtil.now());
//            studentPaperMapper.insert(studentPaper);
//            return Result.success();
//        }else {
//            return Result.error("-1","已提交！");
//        }
//
//    }
//     //改
//    @PutMapping
//    public Result<?> update(@RequestBody StudentPaper studentPaper) {
//        studentPaperMapper.updateById(studentPaper);
//        return Result.success();
//    }
    //统一增和改
    @PostMapping
    public Result<?> saveOrUpdate(@RequestBody StudentPaper studentPaper) {
        if (studentPaper.getId()==null){
            List<StudentPaper> studentPaperList = studentPaperService.list(new QueryWrapper<StudentPaper>().eq("exam_id", studentPaper.getExamId()).eq("user_id", studentPaper.getUserId()));
            if(CollUtil.isNotEmpty(studentPaperList)){
                return Result.error("-1","已提交！");
            }
            studentPaper.setTime(DateUtil.now());
        }
        studentPaperService.saveOrUpdate(studentPaper);
        return Result.success();
    }

    //查一个
    @GetMapping("/{id}")
    public Result<?> getByID (@PathVariable Long id) {
        return Result.success(studentPaperMapper.selectById(id));
    }
    // 删
    @DeleteMapping("/{id}")
    public Result<?> update(@PathVariable Long id) {
        studentPaperMapper.deleteById(id);
        return Result.success();
    }
    //查看某个考试下的所有提交记录
//    @GetMapping("/page/selectExam/{examId}")
//    public Result<?> selectExam(@PathVariable Integer examId) {
//
//        return Result.success(studentPaperMapper.selectList(new QueryWrapper<StudentPaper>().eq("exam_id",examId)));
//    }
    //给当前学生看自己的成绩
    @GetMapping("/page/front/{userId}")
    public Result<?> myGrades(@PathVariable Integer userId) {
        List<Exam> list=studentPaperMapper.selectGrades(userId);
        return Result.success(list);
    }
    //全查（给其他vue用）
    @GetMapping
    public Result<?> findAll(){
        return Result.success(studentPaperService.list());
    }
    //全(页)查
    @GetMapping("/page")
    public Result<?> findPage(@RequestParam(defaultValue = "10") Integer pagesize,
                              @RequestParam(defaultValue = "1") Integer pagenum,
                              @RequestParam(required = false) Integer examId) {
        LambdaQueryWrapper<StudentPaper> wrapper = Wrappers.<StudentPaper>lambdaQuery();
        if (examId!=null) {
            wrapper.eq(StudentPaper::getExamId, examId);
        }
        return Result.success(studentPaperMapper.selectPage(new Page<>(pagenum, pagesize), wrapper));
    }

}
